package com.xjtu.chapter08.thread.producer_consumer;

/**
 * @author yjq
 * @version 1.0
 * @date 2021/8/30 11:30
 */
public class Storage {
    Product[] products = new Product[10];
    static int top = 0;

    //生产者往仓库中放入产品
    public synchronized void push(Product product){
        while (top==products.length){
            try {
                System.out.println("storage is full, producer is waiting");
                wait();
            } catch (InterruptedException e) {
                System.out.println("producer failed to wait");
                e.printStackTrace();
            }
        }
        //把产品放进仓库
        products[top++] = product;
        System.out.println(Thread.currentThread().getName()+" creates "+product);
        notify();//唤醒等待线程
    }
    //消费者从仓库中取出产品
    public synchronized Product pop(){
        while (top==0){
            try{
                System.out.println("storage is empty,consumer is waiting");
                wait();//仓库空，等待
            } catch (InterruptedException e) {
                System.out.println("consumer failed to wait");
                e.printStackTrace();
            }
        }
        //从仓库中取产品
        --top;
        Product p = new Product(products[top].id,products[top].name);
        products[top]=null;
        System.out.println(Thread.currentThread().getName()+" buy "+p);
        notify();
        return p;
    }
//    //生产者往仓库中放入产品
//    public void push(Product product){
//        synchronized (this){
//            //把产品放进仓库
//            products[top++] = product;
//            System.out.println(Thread.currentThread().getName()+" creates "+product);
//            available = true;
//        }
//    }
//    //消费者从仓库中取出产品
//    public Product pop(){
//        synchronized(this){
//            --top;
//            Product p = new Product(products[top].id,products[top].name);
//            products[top]=null;
//            System.out.println(Thread.currentThread().getName()+" buy "+p);
//            available = false;//仓库空了
//            return p;
//        }
//        //从仓库中取产品
//    }
}
